package com.jxdinfo.hussar.cloud.module.example.dljc.basketdatahistory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdatahistory.vo.BasketDataHistoryPageVO;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdatahistory.vo.BasketDataHistorySearchVO;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdatahistory.vo.BasketDataHistoryVO;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.platform.core.utils.BeanUtil;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.platform.core.utils.HussarUtils;
import com.jxdinfo.hussar.support.exception.HussarException;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdatahistory.model.BasketDataHistory;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdatahistory.service.BasketDataHistoryService;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdatahistory.dao.BasketDataHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 吊篮历史数据服务实现类
 */
@Service("dljc.basketdatahistory.BasketDataHistoryServiceImpl")
@HussarDs("master")
public class BasketDataHistoryServiceImpl extends HussarServiceImpl<BasketDataHistoryMapper, BasketDataHistory> implements BasketDataHistoryService {

    private static final Logger logger = LoggerFactory.getLogger(BasketDataHistoryServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private BasketDataHistoryMapper basketDataHistoryMapper;

    @Override
    public ApiResponse<BasketDataHistoryPageVO> findByPage(BasketDataHistorySearchVO basketDataHistorySearchVO) {
        QueryWrapper<BasketDataHistory> wrapper = new QueryWrapper<>();

        Page<BasketDataHistory> page = new Page<>();
        page.setPages(basketDataHistorySearchVO.getPage());
        page.setSize(basketDataHistorySearchVO.getRows());

        if(ObjectUtils.isNotEmpty(basketDataHistorySearchVO.getPointId())) {
            wrapper.eq("point_id", basketDataHistorySearchVO.getPointId());
        }
        if(ObjectUtils.isNotEmpty(basketDataHistorySearchVO.getCollectionTimeStart())) {
            wrapper.ge("collection_time", basketDataHistorySearchVO.getCollectionTimeStart());
        }
        if(ObjectUtils.isNotEmpty(basketDataHistorySearchVO.getCollectionTimeEnd())) {
            wrapper.lt("collection_time", basketDataHistorySearchVO.getCollectionTimeEnd());
        }

        wrapper.orderByAsc("collection_time");

        Page<BasketDataHistory> basketDataHistoryPage = basketDataHistoryMapper.selectPage(page, wrapper);
        BasketDataHistoryPageVO basketDataHistoryPageVO = new BasketDataHistoryPageVO();
        basketDataHistoryPageVO.setCode(RETURN_CODE);
        basketDataHistoryPageVO.setData(basketDataHistoryPage.getRecords());
        basketDataHistoryPageVO.setCount(basketDataHistoryPage.getTotal());

        return ApiResponse.success(basketDataHistoryPageVO);
    }

    @Override
    public ApiResponse<List<BasketDataHistoryVO>> findLineChart(BasketDataHistorySearchVO basketDataHistorySearchVO) {
        QueryWrapper<BasketDataHistory> wrapper = new QueryWrapper<>();

        if(ObjectUtils.isNotEmpty(basketDataHistorySearchVO.getPointId())) {
            wrapper.eq("point_id", basketDataHistorySearchVO.getPointId());
        }
        if(ObjectUtils.isNotEmpty(basketDataHistorySearchVO.getCollectionTimeStart())) {
            wrapper.ge("collection_time", basketDataHistorySearchVO.getCollectionTimeStart());
        }
        if(ObjectUtils.isNotEmpty(basketDataHistorySearchVO.getCollectionTimeEnd())) {
            wrapper.lt("collection_time", basketDataHistorySearchVO.getCollectionTimeEnd());
        }

        wrapper.orderByAsc("collection_time");

        List<BasketDataHistory> basketDataHistories = basketDataHistoryMapper.selectList(wrapper);
        List<BasketDataHistoryVO> basketDataHistoryVOS = BeanUtil.copyToList(basketDataHistories, BasketDataHistoryVO.class);

        return ApiResponse.success(basketDataHistoryVOS);
    }
}
